<?php 
if (!isset($_SESSION)) {
  session_start();
  if(!isset($_SESSION['MM_Username']))
  	header("location:../../index.php");
}
	require_once('../../Connections/microassur.php');
	include_once '../../include/inc_session.php';
	$colname_rs1 = "-1";
	$idContrat = "-1";

	$oContrat = new dataObject_contrat();
	if(isset($_GET['Identifiant_personne']) && isset($_GET['Numero_contrat'])) 
	{
  		$idContrat = $oContrat->contrat_getIdFromNumContrat($_GET['Numero_contrat']);
  		$idPersonne = $_GET['Identifiant_personne'];
	}  
	
$oPersonne = new dataObject_personne($_GET['Identifiant_personne']);
$oContrat = new dataObject_contrat($idContrat);
$aAssure = $oContrat->contrat_getListAssureActif($idContrat);
if(isset($aAssure) && count($aAssure)==1)  header('location:index.php?MM_rechercher=form1&Numero_contrat='.$_GET['Numero_contrat']);

	require_once('../../utils/fonctions.php');
	$message = '';
	$date_du_jour = date('Y-m-d');
	if(isset($_SESSION['iduser'])){
		
	}

/********************************DEBUT FONCTIONS****************************/
function idGarantie_garantiesouscrite($database_microassur, $microassur, $Identifiant_contrat){
	$query_rs = "SELECT Identifiant_garantie FROM garantiesouscrite WHERE Identifiant_contrat = $Identifiant_contrat";
	$rs = mysql_query($query_rs, $microassur) or die(mysql_error());
	$row_rs = mysql_fetch_assoc($rs);
	mysql_free_result($rs);
	return $row_rs['Identifiant_garantie'];
}

function idProduit_garantie($database_microassur, $microassur, $Identifiant_garantie){
	$query_rs = "SELECT Identifiant_produit FROM garantie WHERE Identifiant_garantie = $Identifiant_garantie";
	$rs = mysql_query($query_rs, $microassur) or die(mysql_error());
	$row_rs = mysql_fetch_assoc($rs);
	mysql_free_result($rs);
	return $row_rs['Identifiant_produit'];
}

function idOffre_produit($database_microassur, $microassur, $Identifiant_produit){
	$query_rs = "SELECT Identifiant_offre FROM produit WHERE Identifiant_produit = $Identifiant_produit";
	$rs = mysql_query($query_rs, $microassur) or die(mysql_error());
	$row_rs = mysql_fetch_assoc($rs);
	mysql_free_result($rs);
	return $row_rs['Identifiant_offre'];
}
/********************************FIN FONCTIONS****************************/
?>

<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}


$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if(isset($_GET['modif']))
	$modif = $_GET['modif'];
else
	$modif = 0;
	
  

if (isset($_GET['Identifiant_personne'])) {
  $colname_rs1 = $_GET['Identifiant_personne'];
}
mysql_select_db($database_microassur, $microassur);
$query_rs1 = sprintf("SELECT * FROM personne WHERE Identifiant_personne = %s", GetSQLValueString($colname_rs1, "int"));
$rs1 = mysql_query($query_rs1, $microassur) or die(mysql_error());
$row_rs1 = mysql_fetch_assoc($rs1);
$totalRows_rs1 = mysql_num_rows($rs1);

// ************DEBUT

	
	if(isset($_POST['Date_naissance_personne'])) // && !empty($_POST['Date_naissance_personne'])
	{
		//$dateNaissance = dateEN2FR(remind_text('Date_naissance_personne'));
		$dateNaissance = $_POST['Date_naissance_personne'];

	}		
 	else 
 	{
 		//$dateNaissance = dateEN2FR(dateNaissancePersonne($database_microassur, $microassur,$idPersonne));
 		$dateNaissance = $oPersonne->_DATE_NAISSANCE_PERSONNE;
 	}
		

if(isset($idContrat)){
	$idGarantie = idGarantie_garantiesouscrite($database_microassur, $microassur, GetSQLValueString($idContrat, "int"));
}
if(isset($idGarantie)){
	$idProduit = idProduit_garantie($database_microassur, $microassur, intval($idGarantie));
}
if(isset($idProduit)){
	$idOffre = idOffre_produit($database_microassur, $microassur, $idProduit);
}

//calcule de l'age de la personne
//ON CALCULE L'AGE DE LA PERSONNE
	    
	
		//$nAge = $oPersonne->personne_getAge($oPersonne->_DATE_NAISSANCE_PERSONNE);
		$nAge = $oPersonne->personne_getAge($dateNaissance);

		//var_dump($nAge);

//$age = age($dateNaissance); //var_dump($age);

if(isset($colname_rs1) && isset($idContrat)){
  $typeAssure = typeAssureRole2($database_microassur, $microassur, $colname_rs1, $idContrat);
  //$idOffre = idOffreRole2($database_microassur, $microassur, $colname_rs1, $idContrat);
  $etatRole = etatRole($database_microassur, $microassur, $colname_rs1, $idContrat);
 
}

mysql_select_db($database_microassur, $microassur);
$query_rs2 = "SELECT * FROM conditionindemnisation WHERE Type_condition_indemnisation = 'L' AND Identifiant_offre = '". $idOffre ."' AND Identifiant_offre IS NOT NULL";
$rs21 = mysql_query($query_rs2, $microassur) or die(mysql_error());
$rs2 = mysql_query($query_rs2, $microassur) or die(mysql_error());

$totalRows_rs2 = mysql_num_rows($rs2);




//***********FIN

	$probleme_age = false;
	$oCondIndem = new dataObject_conditionIndemnisation();
//	//$oCdtVente = new dataObject_conditionVente();
	
	$aCdtIndLimit = $oCondIndem->get_CdtIndemnTypeLimiteContrat($idContrat, $oContrat->_DATE_EFFET);
	foreach($aCdtIndLimit as $i => $row_rs2)
	{
			//si le type assuré est un enfant , et si le type assure est adulte
			if(($typeAssure=='E') && (strcmp($row_rs2['Code_condition_indemnisation'], 'AGLIE' )==0) && ($nAge < intval($row_rs2['Valeur_condition_indemnisation'])) )
			{
				$message = colorer(htmlentities(radier_assure_age_pas_atteint, ENT_COMPAT, 'utf-8'), "false");
				$probleme_age = true;
			}
			if(($typeAssure!='E') && (strcmp($row_rs2['Code_condition_indemnisation'], 'AGLIA' )==0) && ($nAge < intval($row_rs2['Valeur_condition_indemnisation'])) ) 
			{
				$message = colorer(htmlentities(radier_assure_age_pas_atteint, ENT_COMPAT, 'utf-8'), "false");
				$probleme_age = true;
			}
	}

    //$probleme_age = false;
	//while($row_rs2 = mysql_fetch_assoc($rs2))
	//{ 
	
	// FS methode remplacée plus bas
	/* if(strcmp($row_rs2['Code_condition_indemnisation'], 'AGLIA' )==0 || strcmp($row_rs2['Code_condition_indemnisation'], 'AGLIE' )==0 )
	 {
		if( $typeAssure='E' && strcmp($row_rs2['Code_condition_indemnisation'], 'AGLIE' )==0 && ($nAge< intval($row_rs2['Valeur_condition_indemnisation'])))
		{
			$message = colorer("La personne n'a pas atteint l'age limite d'adhesion.", "false");
			$probleme_age = true;
		}
		// FS je nai pas vu ce controle dans les cinématiques
		if( $typeAssure!='E' && strcmp($row_rs2['Code_condition_indemnisation'], 'AGLIA' )==0 && ($nAge > intval($row_rs2['Valeur_condition_indemnisation'])))
		{
			$message = colorer("La personne a dépassé l’âge limite d'adhesion.", "false");
			$probleme_age = true;
			 
		}
	 }	
	}*/
	 
	 
		//}
		


	

// Modification  et test sur la date de naissance de la personne

if(isset($_POST['Date_naissance_personne']) && empty($_POST['Date_naissance_personne']))
{
	$message = colorer("Vous devez saisir la date de naissance.", "false");
}
elseif(!empty($_POST['Date_naissance_personne']) && !datevalider($_POST['Date_naissance_personne']))
{
	$message = colorer("Le format de la date de naissance est JJ-MM-SSAA, JJ étant le jour, MM le mois, SS le siècle, AA l’année.", "false");
}
elseif(isset($_POST['Date_naissance_personne']) && !datevalide($_POST['Date_naissance_personne']))
{
	$message = colorer("La date de naissance est incohérente.", "false");
}else
{ 
	if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1") && ($probleme_age == false)  )
	{
			$updateSQL = sprintf("UPDATE personne SET Date_naissance_personne=%s WHERE Identifiant_personne=%s",
		
							   GetSQLValueString(dateEN2FR($dateNaissance), "date"),
							   GetSQLValueString($_GET['Identifiant_personne'], "int"));
			//on modifie la date de naissane
			$Result1 = mysql_query($updateSQL, $microassur) or die(mysql_error());
			if(!$Result1)
			{
				$message = colorer("Problème modification date de naissance, radiation assuré, contactez la DSI.", "false");
			}
			//avec ou sans modification de la date de naissance , on desactive les roles ,l es garanties souscrites		   
			mysql_select_db($database_microassur, $microassur);
			mysql_query("START TRANSACTION");
			  
			$updateSQL2 = sprintf("UPDATE role SET Etat_role='R' WHERE Type_role='A' AND  Identifiant_contrat=%s AND Identifiant_personne=%s",
								GetSQLValueString($idContrat, "int"),
								GetSQLValueString($_GET['Identifiant_personne'], "int"));
			$Result2 = mysql_query($updateSQL2, $microassur) or die(mysql_error());
			if(!$Result2)
			{
					$message = colorer(radiation_assure_role_problem, "false");
			}
			else
			{
				$updateSQL3 = sprintf("UPDATE garantiesouscrite SET Etat_garantie_souscrite='I' WHERE Etat_garantie_souscrite='A' AND Identifiant_contrat=%s AND Identifiant_personne=%s", 
			                     GetSQLValueString($idContrat, "int"),
			                     GetSQLValueString($_GET['Identifiant_personne'], "int"));
				$Result3 = mysql_query($updateSQL3, $microassur) or die(mysql_error());
				if(!$Result3)
				{
					$message = colorer(radiation_assure_garsouscrite_problem, "false");
				}
				else
				{
					//Creation d'evenement	
					//if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form2"))	{
						$insertSQL = sprintf("INSERT INTO evenement (Identifiant_evenement, Type_evenement, Sous_type_evenement, Motif_evenement, Reference_evenement, Date_evenement, Identifiant_agent, Identifiant_sinistre, Identifiant_contrat, Identifiant_personne, Identifiant_echeance, Identifiant_prestation) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
					                       GetSQLValueString($_POST['Identifiant_evenement'], "int"),
					                       GetSQLValueString($_POST['Type_evenement'], "text"),
					                       GetSQLValueString($_POST['Sous_type_evenement'], "text"),
					                       GetSQLValueString($_POST['Motif_evenement'], "text"),
					                       GetSQLValueString($_POST['Reference_evenement'], "text"),
					                       GetSQLValueString($_POST['Date_evenement'], "date"),
					                       GetSQLValueString($_POST['Identifiant_agent'], "int"),
					                       GetSQLValueString($_POST['Identifiant_sinistre'], "int"),
					                       GetSQLValueString($_POST['Identifiant_contrat'], "int"),
					                       GetSQLValueString($_POST['Identifiant_personne'], "int"),
					                       GetSQLValueString($_POST['Identifiant_echeance'], "int"),
					                       GetSQLValueString($_POST['Identifiant_prestation'], "int"));
			
						$Result1 = mysql_query($insertSQL, $microassur) or die(mysql_error());
						if(!$Result1)
						{
						  $message = colorer(radiation_assure_evenement_problem, "false");
						}
						else
						{
							$modif=1;
							mysql_query("commit");
							$message = colorer(radiation_assure_effectue, "false");
						}
					//}
				  	if($modif!=1) 	
				  	{	mysql_query("rollback");
						$message = colorer(radiation_assure_personne_problem, "false");
				  	}
			}
		}
	}
}


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=8" />
			<title>Intervenants</title>
			<link rel="stylesheet" type="text/css" href="../../css/main.css" />
            <link rel="stylesheet" type="text/css" href="list.css" />
			
	</head>

<body>
	<div id="sf_menu1">
		<h3>Production</h3>
		<?php 
			if( isset($message) && !empty($message) ){
				echo $message;
				unset($age);
			} 
			$nommodule="Radiation Assuré";
	$nom_module_saisie="Détail Intervenant";
		?>
	</div>
	<div class="cleared"></div>
	<div class="corps1">
			<!--/***************************************/-->
<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
	<div class="imgleft"></div>
	<div class="imgmiddle">
		<div class="btn">
        	<span class="btnleft"></span>
            <span class="btnmiddle">
            	<h5><?php echo $nommodule; ?></h5>
            </span><span class="btnright"></span>
        </div>
        <div class="btn1">
        	<span class="btnleft"></span>
            <span class="btnmiddle">
            	<h5><?php echo $nom_module_saisie; ?></h5>
            </span><span class="btnright"></span>
        </div>
		<div class="cleared"></div>
		<div class="formulaire">
		  <table class="tabform">
			<tr>
			  <td>NOM</td>
			  <td><input type="text" name="Nom_personne" value="<?php if(isset($_POST['Nom_personne'])){ echo remind_text('Nom_personne'); }else{ echo htmlentities($row_rs1['Nom_personne'], ENT_COMPAT, 'utf-8');} ?>" size="32" disabled="disabled" /></td>
			</tr>
			<tr>
			  <td>PRENOM</td>
			  <td><input type="text" name="Prenom_personne" value="<?php if(isset($_POST['Prenom_personne'])){ echo remind_text('Prenom_personne'); }else{ echo htmlentities($row_rs1['Prenom_personne'], ENT_COMPAT, 'utf-8');} ?>" size="32" disabled="disabled" /></td>
			</tr>
			<tr>
			  <td>TYPE ASSURE</td>
			  <?php $tab_typeAssure = array('A'=>'Ascendant', 'U'=>'Autre', 'C'=>'Conjoint', 'E'=>'Enfant', 'S'=>'Souscripteur'); ?>
			  <td><input type="text" name="Type_assure" value="<?php if(isset($typeAssure)) echo $tab_typeAssure[$typeAssure]; ?>" size="32" disabled="disabled" /></td>
			</tr>
			<tr>
			  <td>DATE DE NAISSANCE</td>
			  <td><input type="text" name="Date_naissance_personne" value="<?php echo $dateNaissance;// if(isset($_POST['Date_naissance_personne'])){ echo remind_text('Date_naissance_personne'); }else{ echo htmlentities(dateEN2FR($row_rs1['Date_naissance_personne'], ENT_COMPAT, 'utf-8'));} ?>"  size="32" maxlength="10" /></td>
			</tr>
			<tr>
			  <td>LIEU DE NAISSANCE</td>
			  <td><input type="text" name="Lieu_naissance_personne" value="<?php  if(isset($_POST['Lieu_naissance_personne'])){ echo remind_text('Lieu_naissance_personne'); }else{ echo htmlentities($row_rs1['Lieu_naissance_personne'], ENT_COMPAT, 'utf-8');} ?>" size="32" disabled="disabled" /></td>
			</tr>
			<!--tr>
			  <td>&nbsp;</td>
			  <td>
				<input type="submit" value="Valider" />&nbsp;
				<a href="index.php?MM_rechercher=form1&Numero_contrat=<?php echo $_GET['Numero_contrat']; ?>&modif=<?php echo $modif; ?>">Annuler</a>&nbsp;
				<a href="index.php?MM_rechercher=form1&Numero_contrat=<?php echo $_GET['Numero_contrat']; ?>&modif=<?php echo $modif; ?>">Retour</a>
			  </td>
			</tr-->
		  </table>
	  </div>
	  <input type="hidden" name="Identifiant_personne" value="<?php if(isset($_POST['Identifiant_personne'])){ echo $_POST['Identifiant_personne']; }else { echo $row_rs1['Identifiant_personne'];} ?>" />
	  <input type="hidden" name="Type_personne" value="<?php echo htmlentities($row_rs1['Type_personne'], ENT_COMPAT, 'utf-8'); ?>" />
	  <input type="hidden" name="Etat_personne" value="<?php echo htmlentities($row_rs1['Etat_personne'], ENT_COMPAT, 'utf-8'); ?>" />
	  <input type="hidden" name="Civilite_personne" value="<?php echo htmlentities($row_rs1['Civilite_personne'], ENT_COMPAT, 'utf-8'); ?>" />
	  <input type="hidden" name="Nom_jeune_fille_personne" value="<?php echo htmlentities($row_rs1['Nom_jeune_fille_personne'], ENT_COMPAT, 'utf-8'); ?>" />
	  <input type="hidden" name="Raison_sociale" value="<?php echo htmlentities($row_rs1['Raison_sociale'], ENT_COMPAT, 'utf-8'); ?>" />
	  <input type="hidden" name="Sexe_personne" value="<?php echo htmlentities($row_rs1['Sexe_personne'], ENT_COMPAT, 'utf-8'); ?>" />
	  <input type="hidden" name="Profession_personne" value="<?php echo htmlentities($row_rs1['Profession_personne'], ENT_COMPAT, 'utf-8'); ?>" />
	  <input type="hidden" name="Employeur_personne" value="<?php echo htmlentities($row_rs1['Employeur_personne'], ENT_COMPAT, 'utf-8'); ?>" />
	  <input type="hidden" name="MM_update" value="form1" />

	</div>
	<div class="imgright"></div>

	<div class="cleared"></div>
	<!--/***************************************/-->
	<div id="br"></div>

  
  <div class="bt1">
	<a href="index.php?MM_rechercher=form1&Numero_contrat=<?php echo $_GET['Numero_contrat']; ?>&modif=<?php echo $modif; ?>">
    	<span class="btleft"></span>
        <span class="btmiddle">Retour</span>
        <span class="btright"></span>
    </a>
  </div>
  <div class="bt">
	<?php if(isset($_GET['modif']) && $_GET['modif']==1){ ?>
	<a href="index.php?MM_rechercher=form1&Numero_contrat=<?php echo $_GET['Numero_contrat']; ?>&modif=1">
	<?php }else{ ?>
	<a href="index.php?MM_rechercher=form1&Numero_contrat=<?php echo $_GET['Numero_contrat']; ?>&modif=<?php echo $modif; ?>">
	<?php } ?>
    	<span class="btleft"></span>
        <span class="btmiddle">Annuler</span>
        <span class="btright"></span>
    </a>
  </div>
   <div class="bt">
	<span class="btleft"></span>
	<input type="submit" name="Valider" value="Valider" class="resizeall btmiddle"/>
	<span class="btright"></span>
  </div>
  
   
  <input type="hidden" name="Identifiant_evenement" value="" />
  <input type="hidden" name="Type_evenement" value="G" /> <!-- G : Acte de gestion -->
  <input type="hidden" name="Sous_type_evenement" value="ARA" /> <!-- ARA : Radiation assure -->
  <input type="hidden" name="Motif_evenement" value="" />
  <input type="hidden" name="Reference_evenement" value="" />
  <input type="hidden" name="Date_evenement" value="<?php echo $date_du_jour ?>" />
  <input type="hidden" name="Identifiant_agent" value="<?php echo $_SESSION['iduser']; ?>" />
  <input type="hidden" name="Identifiant_sinistre" value="" />
  <input type="hidden" name="Identifiant_contrat" value="<?php echo $idContrat; ?>" />
  <input type="hidden" name="Identifiant_personne" value="<?php echo $row_rs1['Identifiant_personne'] ?>" />
  <input type="hidden" name="Identifiant_echeance" value="" />
  <input type="hidden" name="Identifiant_prestation" value="" />
  <input type="hidden" name="MM_insert" value="form2" />
  
  
  
  
  
  
  
  
</form>

 </div>
</body>
</html>
<?php
	mysql_free_result($rs1);
	include_once PATH_INCLUDE.'inc_pied_page.php';	 
?>